{% extends "../layouts/admin.html" %} {% block content %}

<header class="header navbar bg-white shadow">
<div class="btn-group tool-button">
     <a class="btn btn-primary navbar-btn" href="/store/coupon/index" data-pjax><i class="ti-angle-left"></i> 返回优惠券</a>
      <button class="btn btn-danger navbar-btn" onclick="delCheck()"><i class="ti-close"></i> 删除选中</button>
    </div>
	<div class="pull-right offscreen-right">
		<button class="btn btn-primary navbar-btn" onclick="sublime.toggleFilter('.cd-panel')"><i
          class="fa fa-sliders"></i> 筛选
      </button>
	</div>
</header>
<div class=panel-body style="padding-top: 50px;">
	<div class="table-responsive no-border">
		<input id="unitid" type="hidden">
		<table class="table table-bordered table-striped mg-t datatable">
			<thead>
				<tr>
					<th>ID</th>
					<th>用户ID</th>
					<th>优惠券ID</th>
					<th>优惠券编号</th>
					<th>领取时间</th>
					<th>使用时间</th>
					<th>关联订单号</th>

		</table>
	</div>
</div>
<div class="cd-panel from-right">
	<header class="cd-panel-header">
		<h4>高级筛选</h4>
	</header>
	<div class="cd-panel-container">
		<div class="cd-panel-content shadow">
			<div class="form-group">
				<label for="userid">用户ID</label>
				<input type="text" id="userid" name="userid" onclick="this.value=''" class="form-control" placeholder="用户ID">
			</div>
			<div class="form-group">
				<label for="couponid">优惠券ID</label>
				<input type="text" id="couponid" name="couponid" onclick="this.value=''" class="form-control" placeholder="优惠券ID">
			</div>

			<button id="searchBtn" type="button" class="btn btn-default">查询</button>
		</div>
	</div>
</div>
<!-- 删除选中优惠券 -->
<div id="dialogDeleteCheck" class="modal fade bs-modal-lg" tabindex="-2" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h4 class="modal-title">删除优惠券</h4>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-xs-12 mb10">
            优惠券删除后无法恢复，确定删除已选优惠券吗？
          </div>
          <div class="col-xs-12">
            <ul id="checkedUser" class="list-group"></ul>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
        <button id="ok" type="button" class="btn btn-primary" data-loading-text="正在删除...">确 定</button>
      </div>
    </div>
  </div>
</div>
<script language="JavaScript">
	function initDatatable() {
		datatable = $('.datatable').DataTable({
			"dom": '<"toolbar">frtip',
			"searching": false,
			"processing": false,
			"serverSide": true,
			"select": true,
			"ordering": true,
			"language": {
				"url": "/assets/plugins/datatables/cn.json"
			},
			"preDrawCallback": function() {
				sublime.showLoadingbar($(".main-content"));
			},
			"drawCallback": function() {
				sublime.closeLoadingbar($(".main-content"));
			},
			"ajax": {
				"url": "/store/user_coupon/index",
				"type": "post",
				"data": function(d) {
					var couponid = window.location.search.match(/id=([^&]*)/);
					
					d.userid = $('#userid').val();
					
					d.couponid = $('#couponid').val() || (couponid?couponid[1]:'');

				}
			},
			"order": [
				[0, 'desc']
			],
			"columns": [{
					"data": "id",
					"bSortable": true
				}, {
					"data": "user_id",
					"bSortable": true
				},
				{
					"data": "coupon_id",
					"bSortable": true
				},
				{
					"data": "coupon_no",
					"bSortable": true
				},
				{
					"data": "create_time",
					"bSortable": true
				},
				{
					"data": "used_time",
					"bSortable": true
				},
				{
					"data": "order_id",
					"bSortable": true
				}

			],
			"columnDefs": [{
					"render": function(data, type, row) {
						return sublime.datetime(data);
					},
					"targets": 4
				},
				{
					"render": function(data, type, row) {
						if(data == 0) {
							return '无'
						}
						return sublime.datetime(data);
					},
					"targets": 5
				},
				{
					"render": function(data, type, row) {
						if(data == 0) {
							return '无'
						}
						return data;
					},
					"targets": 6
				},
				
			]
		});
datatable.on('click', 'tr', function () {
      $(this).toggleClass('selected');
    });
		$("#searchBtn").on('click', function() {
			datatable.ajax.reload();
		});
	}

	$(function() {
		initDatatable();
	});
	function delCheck() {
    var chks = datatable.rows('.selected').data();
    if (chks.length > 0) {
      var ids = [];
      $.each(datatable.rows('.selected').data(), function (i, n) {
        ids.push(n.id);
      });
      var dialog = $("#dialogDeleteCheck");
      dialog.modal("show");
      dialog.find("#ok").unbind("click");
      dialog.find("#ok").bind("click", function (event) {
        var btn = $(this);
        btn.button("loading");
 
        $.post("/store/user_coupon/delete", {"ids": JSON.stringify(ids)}, function (data) {
          if (data.errno == 0) {
            datatable.ajax.reload(null,false);
          } else {
            Toast.error(data.errmsg);
          }
          btn.button("reset");
          dialog.modal("hide");
        }, "json");
      });
    } else {
      Toast.warning("请先选择要删除的优惠券！");
    }
  }
</script>
{% endblock %}